﻿/*************************************************************************************
 * 文 件 名:   HK_UIFramesAnimator
 * 
 * 描    述:   UI动画序列帧播放控制脚本
 * 
 * 版    本：  V1.0
 * 创 建 者：  京产肠饭
 * 创建时间：  2022/7/30
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

namespace HKTools
{
    public class HK_UIFramesAnimator : MonoBehaviour
    {
        [Header("是否自动开始："), SerializeField] bool isAutoPlay = true;
        [Header("循环停顿时间："), SerializeField] float loopPauseTime;
        [Header("帧率："), SerializeField] float framerate = 25.0f;

        [Header("初始时序列帧："), SerializeField] List<Sprite> inFrame_List = new List<Sprite>();
        [Header("循环序列帧："), SerializeField] List<Sprite> circleFrame_List = new List<Sprite>();
        [Header("结束时序列帧："), SerializeField] List<Sprite> outFrame_List = new List<Sprite>();

        enum AnimStatus
        {
            Running,
            Pause,
            Stop
        }

        enum FrameType
        {
            In,
            Circle,
            Out
        }

        Image image;

        // 当前的序列帧动画
        List<Sprite> curFrame_List = new List<Sprite>();

        AnimStatus status;
        FrameType frameType;

        int frameIndex = 0;

        float time;

        void Awake()
        {
            image = GetComponent<Image>();
        }

        void Start()
        {
            if (isAutoPlay)
                PlayFramesAnim();
        }

        void Update()
        {
            if (status == AnimStatus.Running)
            {
                time += Time.deltaTime;

                if (time > 1f / framerate)
                {
                    frameIndex++;
                    time = 0;
                }

                switch (frameType)
                {
                    case FrameType.In:
                        if (frameIndex >= curFrame_List.Count)
                        {
                            if (circleFrame_List.Count <= 0)
                            {
                                status = AnimStatus.Stop;
                                break;
                            }
                            frameType = FrameType.Circle;
                            frameIndex = 0;
                            curFrame_List.Clear();
                            circleFrame_List.ForEach(item => curFrame_List.Add(item));
                            break;
                        }
                        image.sprite = curFrame_List[frameIndex];
                        break;
                    case FrameType.Circle:
                        image.sprite = curFrame_List[frameIndex];

                        if (frameIndex >= curFrame_List.Count - 1)
                        {
                            status = AnimStatus.Pause;
                            StartCoroutine(AnimPause_IE());
                        }
                        break;
                    case FrameType.Out:
                        image.sprite = curFrame_List[frameIndex];
                        if (frameIndex >= curFrame_List.Count - 1)
                        {
                            status = AnimStatus.Stop;
                        }
                        break;
                    default:
                        break;
                }
            }

            if (Input.GetKeyDown(KeyCode.Alpha1))
            {
                PlayFramesAnim();
            }

            if (Input.GetKeyDown(KeyCode.Alpha2))
            {
                FinishFramesAnim();
            }
        }

        IEnumerator AnimPause_IE()
        {
            yield return new WaitForSeconds(loopPauseTime);
            status = AnimStatus.Running;
            frameIndex = 0;
        }

        /// <summary>
        /// 开始播放序列帧
        /// </summary>
        public void PlayFramesAnim()
        {
            if (inFrame_List.Count > 0)
            {
                curFrame_List.Clear();

                inFrame_List.ForEach(item => curFrame_List.Add(item));
                image.sprite = curFrame_List[0];
                frameIndex = 0;
                frameType = FrameType.In;
                status = AnimStatus.Running;
                return;
            }

            if (circleFrame_List.Count > 0)
            {
                curFrame_List.Clear();

                circleFrame_List.ForEach(item => curFrame_List.Add(item));
                image.sprite = curFrame_List[0];
                frameIndex = 0;
                frameType = FrameType.Circle;
                status = AnimStatus.Running;
                return;
            }

            Debug.LogError("当前UI物体" + gameObject.name + "没有初始化或循环序列帧");
        }

        /// <summary>
        /// 结束序列帧播放
        /// </summary>
        public void FinishFramesAnim()
        {
            if (outFrame_List.Count > 0)
            {
                curFrame_List.Clear();

                outFrame_List.ForEach(item => curFrame_List.Add(item));
                image.sprite = curFrame_List[0];
                frameIndex = 0;
                frameType = FrameType.Out;
                status = AnimStatus.Running;
            }
            else
            {
                Debug.LogError("当前UI物体" + gameObject.name + "没有结束序列帧");
            }
        }
    }
}